﻿#define _CRT_SECURE_NO_WARNINGS
#include"snake.h"
void SetPos(short x, short y)
{
	//获得标准输出设备的句柄
	HANDLE houtput =NULL;
	houtput = GetStdHandle(STD_OUTPUT_HANDLE);
	//定位光标的位置
	COORD pos = { x,y };
	SetConsoleCursorPosition(houtput, pos);
}
void WelcomeToGame()
{
	SetPos(40,12);
	wprintf(L"欢迎来到贪吃蛇小游戏！\n");
	SetPos(45, 15);
	wprintf(L"^o^ ^0^ ^0^\n");
	SetPos(42, 20);
	system("pause");
	system("cls");//清理屏幕
	SetPos(28, 4);
	wprintf(L"-*-*-*-*-*-*游戏规则*-*-*-*-*-*-\n");
	SetPos(28, 7);
	wprintf(L"▲ 用↑ ↓ ← →来控制蛇的移动\n");
	SetPos(28, 10);
	wprintf(L"▲ 按F3加速，F4减速\n");
	SetPos(28, 13);
	wprintf(L"▲ 加速中吃到食物的分数更高！\n");
	SetPos(28, 16);
	wprintf(L"▲ 减速中吃到食物的分数会降低！\n");
	SetPos(28, 19);
	system("pause");
	system("cls");//清理屏幕
}
void GreatMap()
{
	// 上
	int i = 0;
	for (i = 0; i < 29; i++)
	{
		wprintf(L"%lc",WALL);
	}
	// 下
	SetPos(0, 26);
	for (i = 0; i < 29; i++)
	{
		wprintf(L"%lc", WALL);
	}
	// 左
	for (i = 1; i <= 25; i++)
	{
		SetPos(0, i);
		wprintf(L"%lc", WALL);
	}
	// 右
	for (i = 1; i <= 25; i++)
	{
		SetPos(56, i);
		wprintf(L"%lc", WALL);
	}
}
void InitSnake(Snake* ps)
{
	int i = 0;
	SnakeNode* cur = NULL;
	for (i = 0; i < 5; i++)
	{
		cur = (SnakeNode*)malloc(sizeof(SnakeNode));
		if (cur == NULL)
		{
			perror("InitSnake()::malloc()");
			return;
		}
		cur->next = NULL;
		cur->x = POS_X + 2 * i;
		cur->y = POS_Y;

		//头插
		if (ps->psnake == NULL)
		{
			ps->psnake = cur;
		}
		else
		{
			cur->next = ps->psnake;
			ps->psnake = cur;
		}
	}
	//cur = ps->psnake;
	while (cur)
	{
		SetPos(cur->x, cur->y);
		wprintf(L"%lc", BODY);
		cur = cur->next;
	}
	//设置贪吃蛇的属性
	ps->dir = RIGHT;
	ps->score = 0;
	ps->food_weight = 10;
	ps->sleep_time = 200;
	ps->status = OK;
}
void CreateFood(Snake* ps)
{
	int x, y;
	//生成X是2的倍数，x:2~54,y：1~25
again:
	do
	{
		x = rand() % 53 + 2;
		y = rand() % 25 + 1;
	} while (x % 2 != 0);
	//x和y的坐标不能与蛇身坐标冲突
	SnakeNode* cur = ps->psnake;
	while (cur)
	{
		if (x == cur->x || y == cur->y)
		{
			goto again;
		}
		cur = cur->next;
	}
	//创建食物节点
	SnakeNode* pfood = (SnakeNode*)malloc(sizeof(SnakeNode));
	if (pfood == NULL)
	{
		perror("CreateFood()::malloc()");
		return;
	}
	pfood->x = x;
	pfood->y = y;
	pfood->next = NULL;

	SetPos(x, y);
	wprintf(L"%lc", FOOD);
	ps->pfood = pfood;
}
void GameStart(Snake* ps)
{
	//0.先设置窗口大小，再隐藏光标
	system("mode con cols=100 lines=30");
	system("title 贪吃蛇");
	
	HANDLE houtput = GetStdHandle(STD_OUTPUT_HANDLE);
	CONSOLE_CURSOR_INFO cursor_info;
	GetConsoleCursorInfo(houtput, &cursor_info);
	cursor_info.bVisible = false;
	SetConsoleCursorInfo(houtput, &cursor_info);
	//1.打印环境界面和功能介绍
	WelcomeToGame();
	//2.绘制地图
	GreatMap();
	//3.创建蛇
	InitSnake(ps);
	//4.创建食物
	CreateFood(ps);
}
void PrintHelpInfo()
{
	SetPos(64, 12);
	wprintf(L"%ls", L"不能穿墙，不能咬到自己");
	SetPos(64, 14);
	wprintf(L"%ls", L"用 ↑ ↓  ←  → 来控制蛇的移动");
	SetPos(64, 16);
	wprintf(L"%ls", L"按F3加速，F4减速");
	SetPos(64, 18);
	wprintf(L"%ls", L"按ESC退出游戏");
	SetPos(64, 20);
	wprintf(L"%ls", L"按空格暂停游戏");
}
void Pause()
{
	while (1)
	{
		Sleep(200);
		if (KEY_PRESS(VK_SPACE))
		{
			break;
		}
	}
}
int NextIsFood(SnakeNode* pn, Snake* ps)
{
	return (ps->pfood->x == pn->x && ps->pfood->y == pn->y);
}

void EatFood(SnakeNode* pn, Snake* ps)
{
	//头插法
	ps->pfood->next = ps->psnake;
	ps->psnake = ps->pfood;

	//释放下一个位置的节点
	free(pn);
	pn = NULL;

	SnakeNode* cur = ps->psnake;
	//打印蛇
	while (cur)
	{
		SetPos(cur->x, cur->y);
		wprintf(L"%lc", BODY);
		cur = cur->next;
	}
	ps->score += ps->food_weight;

	//重新创建食物
	CreateFood(ps);
}

void NoFood(SnakeNode* pn, Snake* ps)
{
	//头插法
	pn->next = ps->psnake;
	ps->psnake= pn;

	SnakeNode* cur = ps->psnake;
	while (cur->next->next != NULL)
	{
		SetPos(cur->x, cur->y);
		wprintf(L"%lc", BODY);
		cur = cur->next;
	}

	//把最后一个结点打印成空格
	SetPos(cur->next->x, cur->next->y);
	printf("  ");

	//释放最后一个结点
	free(cur->next);

	//把倒数第二个节点的地址置为NULL
	cur->next = NULL;
}
void KillByWall(Snake* ps)
{
	if (ps->psnake->x == 0 || ps->psnake->x == 56 ||
		ps->psnake->y == 0 || ps->psnake->y == 26)
	{
		ps->status = KILL_BY_WALL;
	}
}
void KillBySelf(Snake* ps)
{
	SnakeNode* cur = ps->psnake->next;
	while (cur)
	{
		if (cur->x == ps->psnake->x && cur->y == ps->psnake->y)
		{
			ps->status = KILL_BY_SELF;
			break;
		}
		cur = cur->next;
	}
}
void SnakeMove(Snake* ps)
{
	//创建一个结点，表示蛇即将到的下一个节点
	SnakeNode* pNextNode = (SnakeNode*)malloc(sizeof(SnakeNode));
	if (pNextNode == NULL)
	{
		perror("SnakeMove()::malloc()");
		return;
	}
	switch (ps->dir)
	{
	case UP:
		pNextNode->x = ps->psnake->x;
		pNextNode->y = ps->psnake->y - 1;
		break;
	case DOWN:
		pNextNode->x = ps->psnake->x;
		pNextNode->y = ps->psnake->y + 1;
		break;
	case LEFT:
		pNextNode->x = ps->psnake->x - 2;
		pNextNode->y = ps->psnake->y;
		break;
	case RIGHT:
		pNextNode->x = ps->psnake->x + 2;
		pNextNode->y = ps->psnake->y;
		break;
	}
	//检测下一个坐标处是否是食物
	if (NextIsFood(pNextNode, ps))
	{
		EatFood(pNextNode, ps);
	}
	else
	{
		NoFood(pNextNode, ps);
	}
	//检测蛇是否撞墙
	KillByWall(ps);
	//检测蛇是否撞到自己
	KillBySelf(ps);
}

void GameRun(Snake* ps)
{
	//打印提示信息
	PrintHelpInfo();
	do
	{
		//打印总分数和食物的分值
		SetPos(64, 10);
		printf("总分数:%d\n", ps->score);
		SetPos(64, 11);
		printf("当前食物的分数:%2d\n", ps->food_weight);

		if (KEY_PRESS(VK_UP) && ps->dir != DOWN)
		{
			ps->dir = UP;
		}
		else if (KEY_PRESS(VK_DOWN) && ps->dir != UP)
		{
			ps->dir = DOWN;
		}
		else if (KEY_PRESS(VK_LEFT) && ps->dir != RIGHT)
		{
			ps->dir = LEFT;
		}
		else if (KEY_PRESS(VK_RIGHT) && ps->dir != LEFT)
		{
			ps->dir = RIGHT;
		}
		else if (KEY_PRESS(VK_SPACE))
		{
			Pause();//暂停游戏
		}
		else if (KEY_PRESS(VK_ESCAPE))
		{
			//正常退出游戏
			ps->status = END_NORMAL;
		}
		else if (KEY_PRESS(VK_F3))
		{
			//加速
			if (ps->sleep_time > 80)
			{
				ps->sleep_time -= 30;
				ps->food_weight += 2;
			}
		}
		else if (KEY_PRESS(VK_F4))
		{
			//减速
			if (ps->food_weight > 2)
			{
				ps->sleep_time += 30;
				ps->food_weight -= 2;
			}
		}
		SnakeMove(ps);//蛇走一步的过程
		Sleep(ps->sleep_time);
	} while (ps->status == OK);
}	
void GameEnd(Snake* ps)
{
	SetPos(22, 12);
	switch (ps->status)
	{
	case END_NORMAL:
		wprintf(L"主动结束游戏\n");
		break;
	case KILL_BY_WALL:
		wprintf(L"撞到墙上，游戏结束!\n");
		break;
	case KILL_BY_SELF:
		wprintf(L"撞到了自己，游戏结束!\n");
		break;
	}
	//释放蛇身的链表
	SnakeNode* cur = ps->psnake;
	while (cur)
	{
		SnakeNode* del = cur;
		cur = cur->next;
		free(del);
	}
}
